<?php

function upload($f){
	$info = pathinfo($_SERVER['SCRIPT_URI']);
	$t = $_SERVER['REQUEST_TIME'];
	$prefix = 'upload';

	if(!file_exists($prefix)){
		mkdir($prefix,0777);
	}

	
	$dir = rand(0, 999);;
	$prefix .= '/' . $dir;

	if(!file_exists($prefix)){
		mkdir($prefix,0777);
	}

	$ext = '.jpg';

	$prefix .= '/' . $t . $ext;

	move_uploaded_file($f['tmp_name'], $prefix);

	return $info['dirname'] . '/' . $prefix;
}

$editorIndex = $_REQUEST['index'];
$file = $_FILES['img'];

$path = upload($file);


$script = <<<htmltext
<script type="text/javascript">

var win = window.parent || window.opner;

if(win != null){
	var editor = win.getJEditor({$editorIndex});	
	editor.insertImage('$path',0,0,'','');
	editor.status = true;
}
</script>
htmltext;

echo $script;
